/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示通过自动波特率检测模式1检测通信波特率
  *          检测字采用0x80
  *          检测完后，接收并回发数据
  *
  *          注意：该自动波特率检测功能基于超时定时器，本芯片仅UART1有超时定时器功能
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J12、J13
  *
  *          MCU.RXD @ PA05  <--->   PC.TXD
  *          MCU.TXD @ PA06  <--->   PC.RXD
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_uart.h"
#include "sym32l010_startkit_board.h"

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/
#define BUF_SIZE 16

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
UART_HandleTypeDef huart1;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void Uart_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/
uint8_t TxRxBuf[BUF_SIZE];

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // UART及端口初始化
    Uart_Init();

    //------------------------------------------------------------
    // 检测并设置完成波特率后，接收 BUF_SIZE 个字节并返回 BUF_SIZE 个字节。
    // 请通过串口助手向MCU.PA05发送数据,并从PA06接收数据
    while (1)
    {
        // 进行波特率自适应，检测字固定为0x80
        HAL_UART_AutoBaud1(huart1.Instance);

        // 接收并发送16字节
        HAL_StatusTypeDef Flag = HAL_UART_Receive(&huart1, TxRxBuf, BUF_SIZE, 0);
        if (Flag == HAL_ERROR)
        {
            HAL_UART_CLR_FLAG(&huart1, UART_IT_FLAG_PE | UART_IT_FLAG_FE);
        }
        if (Flag == HAL_OK)
        {
            HAL_UART_Transmit(&huart1, TxRxBuf, BUF_SIZE, 0);
        }
    }
}


/*******************************************************************************
 * @brief  UART初始化
 * @param  None
 * @retval None
 */
static void Uart_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    // 时钟使能
    HAL_SYSCTRL_GPIOA_CLK_ENABLE();
    HAL_SYSCTRL_UART1_CLK_ENABLE();

    // Config PA06 AS UART1.TXD
    AFx_UART1TXD_PA06();
    GPIO_InitStruct.Pins = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    // Config PA05 AS UART1.RXD
    AFx_UART1RXD_PA05();
    GPIO_InitStruct.Pins = GPIO_PIN_5;
    GPIO_InitStruct.Pull = GPIO_PULL_UP;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    // 配置 UART1
    MemClr(&huart1, sizeof(huart1));

    huart1.Instance           = SYM_UART1;
    huart1.Init.ClockSource   = UART_CLKSRC_PCLK;
    huart1.Init.PclkFreq      = 48000000;
    huart1.Init.BaudRate      = 115200;                               // Baud = 115200
    huart1.Init.ParityMode    = UART_PARITYMODE_NONE;                 // 无校验位
    huart1.Init.StopBits      = UART_STOPBITS_1;                      // STOP = 1

    HAL_UART_Init(&huart1);                                           // 初始化UART
    HAL_UART_TXD_Enable(huart1.Instance);                             // 使能UART的TXD功能
    HAL_UART_RXD_Enable(huart1.Instance);                             // 使能UART的RXD功能
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
